package jhh.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jhh.entity.Jmx;
import jhh.service.NewJmx;
import Test.TestDB;


@WebServlet("/CreateJMXServlet")
public class CreateJMXServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String json = request.getParameter("data");
		 ArrayList rows = (ArrayList)Test.JSON.Decode(json);

		    for(int i=0,l=rows.size(); i<l; i++){
		    	HashMap row = (HashMap)rows.get(i);
		    	  String testplan = ToString(row.get("testplan"));
		          String ThreadGroup = ToString(row.get("ThreadGroup"));
		          String sample_error  = ToString(row.get("sample_error"));
		          int loop  = ToInt(row.get("loop"));
		          int num_threads = ToInt(row.get("num_threads"));
		          String HTTPSamplerProxy = ToString(row.get("HTTPSamplerProxy"));
		          String HTTPSampler_domain = ToString(row.get("HTTPSampler_domain"));
		          int HTTPSampler_port = ToInt(row.get("HTTPSampler_port"));
		          String HTTPSampler_path = ToString(row.get("HTTPSampler_path"));
		          String HTTPSampler_method = ToString(row.get("HTTPSampler_method"));

		          Jmx jmx = new Jmx(testplan, ThreadGroup, sample_error, loop, num_threads, HTTPSamplerProxy, HTTPSampler_domain, HTTPSampler_port, HTTPSampler_path, HTTPSampler_method);
		          String TemPath=this.getServletContext().getRealPath("WEB-INF/Temple.xml");
		          String Path=this.getServletContext().getRealPath("upload");
		          NewJmx newJmx=new NewJmx();
		          newJmx.createJmx(jmx, TemPath, Path);
		         
		    }
		    
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
    public int ToInt(Object o){
    	if(o == null) return 0;
    	double d = Double.parseDouble(o.toString());
    	int i = 0;
		i -= d;
		return -i;			
    }    
    public String ToString(Object o){
    	if(o == null) return "";
    	return o.toString();
    }   

}
